package com.suapi.suapiclientsdk.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.suapi.suapiclientsdk.Until.SignUtils;
import com.suapi.suapiclientsdk.model.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 调用接口的客户端
 */

@Data
@Slf4j
public class SuapiClient {

    private String accessKey;

    private String secretKey;

    public static final String GATEWAY_HOST = "http://localhost:8090";

    public SuapiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public String getNameByGet(String requestParam) {
        // 处理传过来的请求参数
        JSONObject jsonObj = JSONObject.parseObject(requestParam);// 将json字符串转换成对象
        String name = jsonObj.get("name").toString();
        log.info("name:{}", name);

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        //String result= HttpUtil.get(GATEWAY_HOST + "/api/name", paramMap);
        //System.out.println(result);
        HttpResponse response = HttpRequest.get(GATEWAY_HOST + "/api/name" + "?" + HttpUtil.toParams(paramMap))
                .addHeaders(getHeaderMap(requestParam))
                .execute();
        return response.body();
    }

    public String getNameByPost(String requestParam) {
        // 处理传过来的请求参数
        JSONObject jsonObj = JSONObject.parseObject(requestParam);// 将json字符串转换成对象
        String name = jsonObj.get("name").toString();
        log.info("name:{}", name);

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("name", name);
        //String result= HttpUtil.post(GATEWAY_HOST + "/api/name", paramMap);
        //System.out.println(result);
        HttpResponse response = HttpRequest.post(GATEWAY_HOST + "/api/name")
                .addHeaders(getHeaderMap(requestParam))
                .form(paramMap)
                .execute();
        return response.body();
    }

    private Map<String,String> getHeaderMap(String body) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
        //不能在服务器之间发送
        //hashMap.put("secretKey", secretKey);
        hashMap.put("nonce", RandomUtil.randomNumbers(5));
        hashMap.put("body", body);
        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
        //签名之后发送
        hashMap.put("sign", SignUtils.sign(body, secretKey));
        return hashMap;
    }

    public String getUserNameByPost(String requestParam) {
        // 处理传过来的请求参数
        //User user = JSONUtil.toBean(requestParam, User.class);
        //String json = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post(GATEWAY_HOST + "/api/name/name")
                .addHeaders(getHeaderMap(requestParam))
                .header("Content-Type", "application/json; charset=utf-8")
                .body(requestParam)
                .execute();
        System.out.println(httpResponse.getStatus());
        System.out.println(httpResponse.body());
        return httpResponse.body();
    }

    public String getRandomImgUrl(){
        String requestParam = "{\"type\":\"" + RandomUtil.randomString(5) + "\"}";
        HttpRequest request = HttpRequest.get(GATEWAY_HOST + "/api/other/random/imageUrl")
                .addHeaders(getHeaderMap(requestParam));
        HttpResponse response = request.execute();
        return response.body();
    }

}

